import com.sun.xml.internal.ws.util.StringUtils;

import java.util.Arrays;

/**
 * 14. 最长公共前缀
 * <p>
 * 编写一个函数来查找字符串数组中的最长公共前缀。
 * 如果不存在公共前缀，返回空字符串 ""。
 * 示例：
 * 输入：strs = ["flower","flow","flight"]
 * 输出："fl"
 * <p>
 * 输入：strs = ["dog","racecar","car"]
 * 输出：""
 */
public class longestCommonPrefix {
    public static void main(String[] args) {
        String[] strs = {"dog", "racecar", "car"};
        String s = longestCommonPrefix_1(strs);
        System.out.println(s);
    }

    /**
     * 自己的解法（笨方法）
     * <p>
     * 1.先找出最短的字符串
     * 2.逐一对比（然后一个个字符消减）
     * <p>
     * 0 ms(100.00%)	39.5 MB(29.22%)
     *
     * @param strs
     * @return
     */
    public static String longestCommonPrefix_1(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        String shortestString = "";
        //1.先找出最短的字符串
        //String shortestString = Arrays.stream(strs).min(String::compareTo).get();
        for (String str : strs) {
            if (shortestString.equals("") || str.length() < shortestString.length()) {
                shortestString = str;
            }
        }
        Boolean isPass = false;
        //2.逐一对比（然后一个个字符消减）
        while (shortestString.length() > 0 && !isPass) {
            for (String str : strs) {
                if (!str.startsWith(shortestString)) {
                    isPass = false;
                    break;
                }
                isPass = true;
            }
            if (!isPass) {
                shortestString = shortestString.substring(0, shortestString.length() - 1);
            }
        }
        if (!isPass) return "";
        return shortestString;
    }

    /**
     * 0 ms（100.00%）	39.5 MB（27.30%）
     *
     * @param strs
     * @return
     */
    public static String longestCommonPrefix_2(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        String prefix = strs[0];
        int count = strs.length;
        for (int i = 1; i < count; i++) {
            prefix = longestCommonPrefix(prefix, strs[i]);
            if (prefix.length() == 0) {
                break;
            }
        }
        return prefix;
    }

    /**
     * 计算出两个字符串的公共前缀
     *
     * @param str1
     * @param str2
     * @return
     */
    public static String longestCommonPrefix(String str1, String str2) {
        int length = Math.min(str1.length(), str2.length());
        int index = 0;
        while (index < length && str1.charAt(index) == str2.charAt(index)) {
            index++;
        }
        return str1.substring(0, index);
    }

    /**
     * 1 ms(69.20%)	39.1 MB(57.60%)
     *
     * @param strs
     * @return
     */
    public static String longestCommonPrefix_3(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        int length = strs[0].length();
        int count = strs.length;
        for (int i = 0; i < length; i++) {
            char c = strs[0].charAt(i);
            for (int j = 1; j < count; j++) {
                if (i == strs[j].length() || strs[j].charAt(i) != c) {
                    return strs[0].substring(0, i);
                }
            }
        }
        return strs[0];
    }

}
